package com.wanke.dao.impl;
// Generated 2016-12-1 14:23:46 by Hibernate Tools 4.0.1.Final

import java.util.List;

import javax.annotation.Resource;
import javax.naming.InitialContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Example;
import org.springframework.stereotype.Repository;

import com.wanke.dao.TeacherDao;
import com.wanke.entity.Teacher;

/**
 * Home object for domain model class Teacher.
 * @see com.wanke.entity.Teacher
 * @author Hibernate Tools
 */
@Repository("teacherDao")
public class TeacherDaoImpl implements TeacherDao {

	private static final Log log = LogFactory.getLog(TeacherDaoImpl.class);

	@Resource(name="sessionFactory")
	 SessionFactory sessionFactory ;

	/* (non-Javadoc)
	 * @see com.wanke.dao.TeacherDao#persist(com.wanke.entity.Teacher)
	 */
	@Override
	public void persist(Teacher transientInstance) {
		log.debug("persisting Teacher instance");
		try {
			sessionFactory.getCurrentSession().persist(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.wanke.dao.TeacherDao#attachDirty(com.wanke.entity.Teacher)
	 */
	@Override
	public void attachDirty(Teacher instance) {
		log.debug("attaching dirty Teacher instance");
		try {
			sessionFactory.getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.wanke.dao.TeacherDao#attachClean(com.wanke.entity.Teacher)
	 */
	@Override
	public void attachClean(Teacher instance) {
		log.debug("attaching clean Teacher instance");
		try {
			sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.wanke.dao.TeacherDao#delete(com.wanke.entity.Teacher)
	 */
	@Override
	public void delete(Teacher persistentInstance) {
		log.debug("deleting Teacher instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.wanke.dao.TeacherDao#merge(com.wanke.entity.Teacher)
	 */
	@Override
	public Teacher merge(Teacher detachedInstance) {
		log.debug("merging Teacher instance");
		try {
			Teacher result = (Teacher) sessionFactory.getCurrentSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.wanke.dao.TeacherDao#findById(int)
	 */
	@Override
	public Teacher findById(int id) {
		log.debug("getting Teacher instance with id: " + id);
		try {
			Teacher instance = (Teacher) sessionFactory.getCurrentSession().get("com.wanke.entity.Teacher", id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.wanke.dao.TeacherDao#findByExample(com.wanke.entity.Teacher)
	 */
	@Override
	public List findByExample(Teacher instance) {
		log.debug("finding Teacher instance by example");
		try {
			List results = sessionFactory.getCurrentSession().createCriteria("com.wanke.entity.Teacher")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: " + results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
	
	public Teacher findByUserPass(String username,String password){
		String hql="from Teacher t where t.name=? and t.password=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, username);
		query.setString(1, password);
		if(query.list().size()>0){
			return (Teacher)query.list().get(0);
		}
		return null;
		
	}
}
